package com.atguigu.ggkt.vod.service.impl;


import com.atguigu.ggkt.model.vod.Chapter;
import com.atguigu.ggkt.model.vod.Video;
import com.atguigu.ggkt.vo.vod.ChapterVo;
import com.atguigu.ggkt.vo.vod.VideoVo;
import com.atguigu.ggkt.vod.mapper.ChapterMapper;
import com.atguigu.ggkt.vod.service.ChapterService;
import com.atguigu.ggkt.vod.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zfj
 * @since 2022-07-09
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

	@Autowired
	private VideoService videoService;

	@Override
	public List<ChapterVo> getNestedTreeList(Long courseId) {
		//定义最终数据list集合
		List<ChapterVo> chapterVoList = new ArrayList<>();
		//根据courseId获取课程所有章节
		//获取章信息
		LambdaQueryWrapper<Chapter> queryWrapperChapter = new LambdaQueryWrapper<>();
		queryWrapperChapter.eq(Chapter::getCourseId, courseId);
		queryWrapperChapter.orderByAsc(Chapter::getSort, Chapter::getId);
		List<Chapter> chapterList = baseMapper.selectList(queryWrapperChapter);

		//根据courseId获取课程里面所有的小节
		LambdaQueryWrapper<Video> queryWrapperVideo = new LambdaQueryWrapper<>();
		queryWrapperVideo.eq(Video::getCourseId, courseId);
		queryWrapperVideo.orderByAsc(Video::getSort, Video::getId);
		List<Video> videoList = videoService.list(queryWrapperVideo);

		//封装章节
		//填充列表数据：Chapter列表
		for (int i = 0; i < chapterList.size(); i++) {
			Chapter chapter = chapterList.get(i);

			//创建ChapterVo对象
			ChapterVo chapterVo = new ChapterVo();
			BeanUtils.copyProperties(chapter, chapterVo);
			chapterVoList.add(chapterVo);

			//填充列表数据：Video列表
			List<VideoVo> videoVoList = new ArrayList<>();
			for (int j = 0; j < videoList.size(); j++) {
				Video video = videoList.get(j);
				if(chapter.getId().equals(video.getChapterId())){

					VideoVo videoVo = new VideoVo();
					BeanUtils.copyProperties(video, videoVo);
					videoVoList.add(videoVo);
				}
			}
			chapterVo.setChildren(videoVoList);
		}

		//封装小节

		return chapterVoList;
	}

	@Override
	public void removeChapterByCourseId(Long id) {
		QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
		wrapper.eq("course_id",id);
		baseMapper.delete(wrapper);
	}
}
